﻿Imports System.Math
Public Class DPotClass
    Public PotName As String
    Public B, L, H As Double
    Public X, Y, Z As Double
    Public flag As Integer = 0

    Public Sub BLHToXYZ()
        Dim mN As Double = Getf(B)(0)
        X = (mN + H) * Cos(B) * Cos(L)
        Y = (mN + H) * Cos(B) * Sin(L)
        Z = (mN * (1 - e2) + H) * Sin(B)
    End Sub

    Public Sub XYZToBLH()
        Dim mN As Double
        L = Atan(Y / X)
        If L < 0 Then
            L += PI
        End If
        Dim Bs As Double
        Bs = Atan(Z / Sqrt(X ^ 2 + Y ^ 2))
        mN = Getf(Bs)(0)
        B = Atan((Z + mN * e2 * Sin(Bs)) / Sqrt(X ^ 2 + Y ^ 2))
        Do Until Abs(B - Bs) < 0.00000001
            Bs = B
            mN = Getf(Bs)(0)
            B = Atan((Z + mN * e2 * Sin(Bs)) / Sqrt(X ^ 2 + Y ^ 2))
        Loop
        H = Sqrt(X ^ 2 + Y ^ 2) / Cos(B) - mN
    End Sub
    Public Sub BLToXY()
        Dim ml As Double = L - L0
        Dim ma() As Double = GetArray2(B)
        X = ma(0) * Pow(ml, 0) + ma(2) * Pow(ml, 2) + ma(4) * Pow(ml, 4) + ma(6) * Pow(ml, 6)
        Y = ma(1) * Pow(ml, 1) + ma(3) * Pow(ml, 3) + ma(5) * Pow(ml, 5)
        Y = Y + 500000
    End Sub
    Public Sub XYToBL()
        Dim ArraymB() As Double = GetArray3(Getf(GetBf(X)))
        Y -= 500000
        B = ArraymB(0) + ArraymB(2) * Pow(Y, 2) + ArraymB(4) * Pow(Y, 4) + ArraymB(6) * Pow(Y, 6)
        L = ArraymB(1) * Y + ArraymB(3) * Pow(Y, 3) + ArraymB(5) * Pow(Y, 5) + L0
    End Sub

End Class
